<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
              :rules="rules"
              :model="userInfo"
              ref="userForm"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input style="width: 300px" v-model="userInfo.username" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input
                  style="width: 300px"
                  type="password"
                  v-model="userInfo.password2"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- <user-info /> -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="userComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="jobComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import { saveUserDetailById } from "@/api/employees";
import { getUserDetailById } from "@/api/user";
import userInfo from "./components/user-info.vue";
import jobInfo from "./components/job-info.vue";
export default {
  components: {
    userInfo,
    jobInfo,
  },
  data() {
    return {
      userComponent: "userInfo",
      jobComponent: "jobInfo",
      userId: this.$route.params.id,
      userInfo: {
        username: "",
        password2: "", //为什么叫password2,因为后端返回回来的数据密码是加密的，我们不嫩直接读取，而是先把更改的密码传给后端，这里password2就是我们更改后的密码了
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 9,
            message: "密码长度6~9位",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getUserDetailById();
  },
  methods: {
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.userId);
    },
    async saveUser() {
      this.$refs.userForm.validate(); //效验结果
      await saveUserDetailById({
        ...this.userInfo,
        password: this.userInfo.password2,
      });
      this.$message.success("用户信息修改成功");
    },
  },
};
</script>